/*
 * @Descripttion: 
 * @version: 
 * @Author: Zuo
 * @Date: 2021-09-30 09:53:34
 * @LastEditors: Zuo
 * @LastEditTime: 2021-09-30 13:44:45
 */
import { StateProps } from "../../types"

const SET_TOKEN = 'SET_TOKEN',
      SET_USERNAME = 'SET_USERNAME',
      SET_ID = 'SET_ID',
      SET_ROLEID = 'SET_ROLEID',
      REMOVE_TOKEN = 'REMOVE_TOKEN'
export default {
    state:()=>(
        {
            userName:'测试用户',
            token:'',
            id: 0,
            roleId: 0,
        }
    ),
    mutations:{
        [SET_TOKEN]:(state:StateProps,data:string) => {state.token = data;sessionStorage.setItem('token',data)},
        [SET_USERNAME]:(state:StateProps,data:string) => state.userName = data,
        [SET_ID]:(state:StateProps,data:number) => state.id = data,
        [SET_ROLEID]:(state:StateProps,data:number) => state.roleId = data,
        [REMOVE_TOKEN]:(state:StateProps,data:string) => {state.token = data;sessionStorage.removeItem('token')},
    },
    actions:{
        login({commit}:any,data:any){
            commit(SET_TOKEN,data.token)
            commit(SET_USERNAME,data.userName)
            commit(SET_ID,data.id)
            commit(SET_ROLEID,data.roleId)
        },
        logout({commit}:any){
            commit(REMOVE_TOKEN,'')
            commit(SET_USERNAME,'')
            commit(SET_ID,'')
            commit(SET_ROLEID,'')
        }
    },
    getters:{
        token: (state: StateProps) => state.token,
        userName: (state: StateProps) => state.userName,
        id: (state: StateProps) => state.id,
        roleId: (state: StateProps) => state.roleId,
    }
}